package com.huayue.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.Room;
import com.huayue.service.RoomService;
import com.huayue.util.DateHelper;
import com.huayue.util.PageHelper;

/**
 * 客房信息
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    RoomService svc;

    BaseController bc = new BaseController();

    @LogAnno(operateType = "添加-客房信息")
    @RequestMapping("/add")
    public ModelAndView add(Room room) {
        return bc.add(svc, room);
    }

    @LogAnno(operateType = "修改-客房信息")
    @RequestMapping("/update")
    public ModelAndView update(Room room) {
        return bc.update(svc, room);
    }

    @LogAnno(operateType = "删除-客房信息")
    @RequestMapping("/delete")
    public ModelAndView delete(Room room) {
        return bc.delete(svc, room);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(Room room) {
        return bc.findOne(svc, room);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, Room room) {
        return bc.findPage(svc, page, room, room);
    }

    @RequestMapping("/room_type")
    public ModelAndView findRoom_Type() {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("roomtype", svc.findRoom_type());
        return new ModelAndView(view);
    }

    @RequestMapping("/findTotalPrice")
    public ModelAndView findTotalPrice(PageHelper page, Room room, String dateRange) {
        FastJsonJsonView view = new FastJsonJsonView();
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }
        PageHelper pages = new PageHelper();
        pages.setCount(svc.findTotalPriceByRoomCount(room, begin, end));
        pages.setPageNow(page.getPageNow() == 0 ? 1 : Integer.valueOf(page.getPageNow()));
        pages.setPageSize(page.getPageSize() == 0 ? 5 : Integer.valueOf(page.getPageSize()));
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("message", "查询成功");
        view.addStaticAttribute("count", pages.getCount());
        view.addStaticAttribute("data", svc.findTotalPriceByRoom(pages, room, begin, end));
        return new ModelAndView(view);
    }
}
